package com.example.ioc.context;

import com.example.common.utils.Statistics;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;

@Slf4j
//@Component 可以用Component注入
public class LMergedBeanDefinitionPostProcessor implements MergedBeanDefinitionPostProcessor {

    private volatile boolean isLogged = false;

    private static volatile LMergedBeanDefinitionPostProcessor instance;

    private LMergedBeanDefinitionPostProcessor() {
        Statistics.logInfo(log);
    }

    public static LMergedBeanDefinitionPostProcessor getInstance() {
        if (instance == null) {
            synchronized (LMergedBeanDefinitionPostProcessor.class) {
                if (instance == null) {
                    instance = new LMergedBeanDefinitionPostProcessor();
                }
            }
        }
        return instance;
    }

    @Override
    public void postProcessMergedBeanDefinition(@NonNull RootBeanDefinition beanDefinition, @NonNull Class<?> beanType,
                                                @NonNull String beanName) {
        if (!isLogged){
            Statistics.logInfo(log, "postProcessMergedBeanDefinition", beanDefinition);
            isLogged = true;
        }
    }
}
